#define _CONTROL_C_

/* Files include */
#include <stdio.h>
#include "platform.h"
#include "Control.h"
#include "timer1.h"
#include "gpio.h"
#include "multi_button.h"
#include "LedDis.h"
#include "flash_dataspace.h"
#include "AdSample.h"

#define BTN1_ID 0
// uint8_t btn1_id = 0;
struct Button btn1;

uint32_t u32_GetADTimer = 0;
// 工程菜单设置参数
uint8_t u8_Password_P0 = 0;
signed char s8_SetTemp_C0 = 0;
uint8_t u8_TempUpReturnDiff_C1 = 0;
uint8_t u8_TempDownReturnDiff_C2 = 0;
uint8_t u8_CompressorOffTime_C3 = 0;
uint8_t u8_Compressor1stOffTime_C4 = 0;
signed char s8_SetTempUp_C5 = 0;
signed char s8_SetTempDown_C6 = 0;
signed char s8_TempComp_C7 = 0;
uint8_t u8_SensorErrCompressorOnTime_C8 = 0;
uint8_t u8_SensorErrCompressorOffTime_C9 = 0;
uint8_t u8_DefrostCycle_D1 = 0;
uint8_t u8_DefrostTime_D2 = 0;
signed char s8_HighTempErr_A1 = 0;
signed char s8_LowTempErr_A2 = 0;
uint8_t u8_AboveTempContinueTime_A3 = 0;
uint8_t u8_DisplayMode_F1 = 0;
uint8_t u8_DisplayMode_F2 = 0;
uint8_t u8_DisplayTime_F3 = 0;
// 工程菜单设置参数

signed char s8_SetTempC0Display = 0;
uint8_t u8_CompressorState = 0xFF; // 压机状态
uint16_t u16_RealTemp = 0;
uint8_t u8_Compressor1stOnTimeFlag = 0; // 第一次上电开机时间
uint8_t u8_CompressorOffTimeFlag = 0;   // 最小关机时间标志
uint8_t u8_DefrostState = 0;            // 除霜模式
uint8_t u8_OpenCompressorTempFlag = 0;  // 开机点标志
uint8_t u8_CloseCompressorTempFlag = 0; // 关机点标志
uint8_t u8_HighTempErrFlag = 0;         // 高温报警标志
uint8_t u8_LowTempErrFlag = 0;          // 低温报警标志
// uint8_t u8_AboveTempContinueTimeFlag = 0; // 超温到时间标志
uint8_t u8_SensorErrFlag = 0;      // 传感器故障标志
uint8_t u8_SensorErrDelayTime = 0; // 传感器故障报警缓冲时间
uint8_t u8_SensorErrCompOnOff = 0; // 传感器故障压机开/关//这个跟u8_SensorErrFlag设置1时一起设置
uint16_t u16_SetTemp2AD_C0 = 0;    // 设定的温度数字转成AD值
uint16_t u16_500msDelay = 0;       // 2HZ按键快速加功能
uint8_t u8_MenuTimeOutExeFlag = 0; // 菜单显示时间到后处理流程
uint8_t u8_PowerOn = 1;            // 第一次上电
uint8_t u8_Time1SState = 0;        // 1S时基刷新 1HZ
uint8_t u8_Time500msState = 0;     // 500时基刷新 1HZ
uint8_t u8_Time250msState = 0;     // 500时基刷新 0.5HZ
uint8_t u8_LedSnowState = 0;
uint8_t u8_TempIsSet = 0;
uint8_t u8_GetSetTemp = 0;
uint8_t u8_LongPress2ndActive = 0; // 第二级菜单长按键被释放才能设置数字

const uint16_t u16_SetTempADTable[] = {
    232, 240, 249, 257, 267,                                    // 55-51
    276, 286, 297, 307, 319, 330, 342, 355, 368, 382,           // 50-41
    396, 410, 425, 441, 457, 474, 492, 511, 529, 549,           // 40-31
    569, 591, 613, 635, 659, 683, 709, 735, 762, 790,           // 30-21
    819, 849, 879, 911, 944, 978, 1013, 1049, 1086, 1125,       // 20-11
    1164, 1204, 1245, 1288, 1331, 1375, 1421, 1467, 1515, 1563, // 10-1
    1612,                                                       // 0
    1662, 1712, 1764, 1815, 1868, 1921, 1974, 2028, 2083, 2137, //-1--10
    2192, 2247, 2301, 2356, 2410, 2464, 2518, 2572, 2625, 2678, //-11--20
    2730, 2781, 2832, 2882, 2930, 2978, 3025, 3071, 3116, 3160, //-21--30
    3202, 3244, 3303, 3323, 3361, 3397, 3433, 3467, 3499, 3531, //-31--40
};

const signed char s8_SetTempTable[] = {
    55, 54, 53, 52, 51,                               // 51  -- 55
    50, 49, 48, 47, 46, 45, 44, 43, 42, 41,           // 41  -- 50
    40, 39, 38, 37, 36, 35, 34, 33, 32, 31,           // 31  -- 40
    30, 29, 28, 27, 26, 25, 24, 23, 22, 21,           // 21  -- 30
    20, 19, 18, 17, 16, 15, 14, 13, 12, 11,           // 11  -- 20
    10, 9, 8, 7, 6, 5, 4, 3, 2, 1,                    // 1   -- 10
    0,                                                // 0
    -1, -2, -3, -4, -5, -6, -7, -8, -9, -10,          //-10  -- -1
    -11, -12, -13, -14, -15, -16, -17, -18, -19, -20, //-20  -- -11
    -21, -22, -23, -24, -25, -26, -27, -28, -29, -30, //-30  -- -21
    -31, -32, -33, -34, -35, -36, -37, -38, -39, -40  //-40  -- -31
};

#define SET_TEMP_TABLE_SIZE sizeof(s8_SetTempTable)

uint16_t look_up_Temtable(signed char *a, uint16_t ArrayLong, signed char data)
{
    uint16_t begin, end, middle;
    uint16_t i;
    begin = 0;
    end = ArrayLong - 1;
    i = 0;
    if (data >= a[begin])
        return begin;
    else if (data <= a[end])
        return end;
    while (begin < end)
    {
        middle = (begin + end) / 2;
        if (data == a[middle])
            break;
        if (data < a[middle] && data > a[middle + 1])
            break;
        if (data > a[middle])
            end = middle;
        else
            begin = middle;
        if (i++ > ArrayLong)
            break;
    }
    if (begin > end)
        return 0;
    return middle;
}

void GetSetTemp2AD(void)
{
    uint16_t TableIndex = 0;
    if (u8_GetSetTemp)
    {
        u8_GetSetTemp = 0;
        TableIndex = look_up_Temtable((signed char *)s8_SetTempTable, SET_TEMP_TABLE_SIZE, s8_SetTemp_C0);
        u16_SetTemp2AD_C0 = u16_SetTempADTable[TableIndex];
    }
}

void TimeBaseFunRefresh(void)
{
    if (u8_Time250msState)
    {
        u8_Time250msState = 0;
        if (u8_SonwLedFlashFlag == SONW_LED_FLASH_05HZ)
        {
            u8_SonwLedFlashCycle = !u8_SonwLedFlashCycle;
        }
        // if (u8_SensorErrDelayTime > 0)
        // {
        //     u8_SensorErrDelayTime++;
        //     if (u8_SensorErrDelayTime > SENSOR_ERR_DELAY_TIME) // 传感器故障报警
        //     {
        //         if (u8_HighTempErrFlag == 1 || u8_LowTempErrFlag == 1) // 传感器故障报警就取消超温报警
        //         {
        //             u8_HighTempErrFlag = 0;
        //             u8_LowTempErrFlag = 0;
        //         }

        //         u8_SensorErrDelayTime = 0;
        //         u8_SensorErrFlag = 1;
        //         u8_SensorErrCompOnOff = 1;

        //         // u8_LedMenuPageIndex = MENU_PAGE_ERR;
        //     }
        // }
    }

    if (u8_Time500msState)
    {
        u8_Time500msState = 0;

        if (u8_MenuFlahsFlag)
        {
            u8_MenuFlahsCycle = !u8_MenuFlahsCycle;
        }
        if (u8_SonwLedFlashFlag == SONW_LED_FLASH_1HZ)
        {
            u8_SonwLedFlashCycle = !u8_SonwLedFlashCycle;
        }
    }

    if (u8_Time1SState)
    {
        u8_Time1SState = 0;
        if (u8_DisplayMode_F2 == 0) // 显示模式1 显示实际温度
        {
            u8_DisplayTempValueRenewTime = 1;
        }
        else if (u8_DisplayMode_F2 == 1) // 显示模式2
        {
            if (u16_DisplayF2M2ModifyTime > 0)
            {
                u16_DisplayF2M2ModifyTime++;
                if (u16_DisplayF2M2ModifyTime > (8 * 60))
                {
                    u8_DisplayF2M2ModifyTimeState = 3;
                }
                else if (u16_DisplayF2M2ModifyTime > (5 * 60))
                {
                    if ((u16_DisplayF2M2ModifyTime % 10) == 0)
                    {
                        u8_DisplayF2M2ModifyTimeState = 2;
                    }
                }
                else if ((u16_DisplayF2M2ModifyTime % 60) == 0)
                {
                    u8_DisplayF2M2ModifyTimeState = 1;
                }
            }
        }
        else // 显示模式3
        {
        }

        if (u8_NoKeyPressFlashMenuTime > 0)
        {
            u8_NoKeyPressFlashMenuTime--;
            if (u8_NoKeyPressFlashMenuTime == 0)
            {
                if (u8_LedMenuPageIndex == MENU_PAGE_SET         // 设置菜单闪
                    || u8_LedMenuPageIndex == MENU_PAGE_PASSWORD // 设置菜单闪
                    || u8_LedMenuPageIndex == MENU_PAGE_FACTORY)
                    u8_MenuFlahsFlag = 1;
            }
        }

        if (u16_LedMenuDisTime > 0)
        {
            u16_LedMenuDisTime--;
            if (u16_LedMenuDisTime == 0)
            {
                u8_MenuTimeOutExeFlag = 1;
            }
        }

        if (u16_1stCompOnWaitTime2S > 0) // 压机第一次上电开机延迟
        {
            u16_1stCompOnWaitTime2S++;
            // if (u8_OpenCompressorTempFlag == 1)
            // {
            //     u8_SonwLedFlashFlag = SONW_LED_FLASH_1HZ;
            // }
            if ((u16_1stCompOnWaitTime2S - 1) == ((u8_Compressor1stOffTime_C4 * 10) + 1))
            {
                u16_1stCompOnWaitTime2S = 0;
                u8_Compressor1stOnTimeFlag = 1;
                // u8_SonwLedFlashFlag = SONW_LED_OFF;
            }
        }
        else
        {
            if (u8_CompressorState == 1) // 压机开机累计时间
            {
                u32_CompOnAccumulateTime2S++;                                     // 累计开机时间计时
                if (u32_CompOnAccumulateTime2S == HOUR2SECON(u8_DefrostCycle_D1)) // 除霜周期时间开始除霜
                // if (u32_CompOnAccumulateTime2S == 30)
                {
                    u32_CompOnAccumulateTime2S = 0;
                    u8_DefrostState = 1;
                    u32_DefrostTime2S = 0;
                    u8_SonwLedFlashFlag = SONW_LED_FLASH_05HZ;
                }
            }
            else // 压机停机时间
            {
                if (u16_CompressorOffTime2S > 0)
                {
                    u16_CompressorOffTime2S++;
                    if ((u8_DefrostState == 0)                                                 // 压机闪必须在非化霜状态
                        && ((u8_OpenCompressorTempFlag == 1) || (u8_SensorErrCompOnOff == 1))) // 闪灯的条件需要在要开机点和传感器故障点开机时才闪
                    {
                        u8_SonwLedFlashFlag = SONW_LED_FLASH_1HZ;
                    }
                    if ((u16_CompressorOffTime2S - 1) == (MINUTE2SECON(u8_CompressorOffTime_C3) + 1))
                    {
                        if (u8_DefrostState == 0)
                        {
                            u8_SonwLedFlashFlag = SONW_LED_OFF;
                        }
                        u16_CompressorOffTime2S = 0;
                        u8_CompressorOffTimeFlag = 1;
                    }
                }
            }
        }

        if (u8_DefrostState == 1) // 除霜时间
        {
            u32_DefrostTime2S++;
            if (u32_DefrostTime2S == MINUTE2SECON(u8_DefrostTime_D2))
            // if (u32_DefrostTime2S == 30)
            {
                u32_DefrostTime2S = 0;
                u8_DefrostState = 0;
                u8_SonwLedFlashFlag = SONW_LED_OFF;
            }
        }

        if (u16_HighTempErrTime2S > 0) // 超温报警
        {
            u16_HighTempErrTime2S++;

            if ((u16_HighTempErrTime2S - 1) == (MINUTE2SECON(u8_AboveTempContinueTime_A3) + 1))
            {
                u16_HighTempErrTime2S = 0;
                u8_HighTempErrFlag = 1;
                // u8_LedMenuPageIndex = MENU_PAGE_ERR;
                //  u8_AboveTempContinueTimeFlag = 1;
            }
        }
        else if (u16_LowTempErrTime2S > 0)
        {
            u16_LowTempErrTime2S++;
            if ((u16_LowTempErrTime2S - 1) == (MINUTE2SECON(u8_AboveTempContinueTime_A3) + 1))
            {
                u16_LowTempErrTime2S = 0;
                u8_LowTempErrFlag = 1;
                // u8_LedMenuPageIndex = MENU_PAGE_ERR;
                //  u8_AboveTempContinueTimeFlag = 1;
            }
        }

        if (u8_SensorErrFlag)
        {
            if (u8_CompressorOffTime_C3 != 0)
            {
                u16_SensorErrCompOnOffTime++;
                if (u8_SensorErrCompOnOff == 1) // 传感器故障压机开
                {
                    if (u16_SensorErrCompOnOffTime == MINUTE2SECON(u8_SensorErrCompressorOnTime_C8))
                    {
                        u16_SensorErrCompOnOffTime = 0;
                        u8_SensorErrCompOnOff = 0;
                        // if (u16_CompressorOffTime2S == 0)
                        // {
                        //     u8_CompressorOffTimeFlag = 0;
                        //     u16_CompressorOffTime2S = 1;
                        // }
                    }
                }
                else // 传感器故障压机关
                {
                    if (u16_SensorErrCompOnOffTime == MINUTE2SECON(u8_SensorErrCompressorOffTime_C9))
                    {
                        u16_SensorErrCompOnOffTime = 0;
                        u8_SensorErrCompOnOff = 1;
                    }
                }
            }
            else
            {
                u8_SensorErrCompOnOff = 0;
            }
        }
    }
}
// signed char TestTemp1 = 0;
// signed char TestTemp2 = 0;
void GetSystemState(void)
{
    // GetSetTemp2AD();
    if ((u16_stADRoomTemp > SENSOR_AD_MAX) || (u16_stADRoomTemp < SENSOR_AD_MIN)) // 传感器故障报警
    {
        if (u8_SensorErrFlag == 0 && u8_SensorErrDelayTime == 0)
        {
            u8_SensorErrDelayTime = 1;
        }
        else if (u8_SensorErrDelayTime > 0)
        {
            u8_SensorErrDelayTime++;
            if (u8_SensorErrDelayTime > SENSOR_ERR_DELAY_TIME) // 传感器故障报警
            {
                if (u8_HighTempErrFlag == 1 || u8_LowTempErrFlag == 1) // 传感器故障报警就取消超温报警
                {
                    u8_HighTempErrFlag = 0;
                    u8_LowTempErrFlag = 0;
                }

                u8_SensorErrDelayTime = 0;
                u8_SensorErrFlag = 1;
                u8_SensorErrCompOnOff = 1;
                // u8_LedMenuPageIndex = MENU_PAGE_ERR;
            }
        }
    }
    else if (((s8_RealTemp > s8_HighTempErr_A1) || (s8_RealTemp < s8_LowTempErr_A2)) && (u8_DisplayMode_F2 != 1)) // 超温报警
    {
        if (u8_SensorErrFlag == 1) // 传感器故障 // 超温故障
        {
            u8_SensorErrFlag = 0;
            u8_LedMenuPageIndex = MENU_PAGE_MAIN;
            u16_LedMenuDisTime = u8_DisplayTime_F3;
        }

        if ((s8_RealTemp > s8_HighTempErr_A1))
        {
            if (u16_HighTempErrTime2S == 0 && u8_HighTempErrFlag == 0)
            {
                u16_HighTempErrTime2S = 1;
                u8_LowTempErrFlag = 0;
            }
        }
        else if (s8_RealTemp < s8_LowTempErr_A2)
        {
            if (u16_LowTempErrTime2S == 0 && u8_LowTempErrFlag == 0)
            {
                u16_LowTempErrTime2S = 1;
                u8_HighTempErrFlag = 0;
            }
        }
    }
    else if (((s8_DisplayTempValue > s8_HighTempErr_A1) || (s8_DisplayTempValue < s8_LowTempErr_A2)) && (u8_DisplayMode_F2 == 1)) // 修正超温报警
    {
        if (u8_SensorErrFlag == 1) // 传感器故障 // 超温故障
        {
            u8_SensorErrFlag = 0;
            u8_LedMenuPageIndex = MENU_PAGE_MAIN;
            u16_LedMenuDisTime = u8_DisplayTime_F3;
        }

        if ((s8_DisplayTempValue > s8_HighTempErr_A1))
        {
            if (u16_HighTempErrTime2S == 0 && u8_HighTempErrFlag == 0) // 超温报警计时开始
            {
                u16_HighTempErrTime2S = 1;
                u8_LowTempErrFlag = 0;
            }
        }
        else if (s8_DisplayTempValue < s8_LowTempErr_A2) // 超温报警计时开始
        {
            if (u16_LowTempErrTime2S == 0 && u8_LowTempErrFlag == 0)
            {
                u16_LowTempErrTime2S = 1;
                u8_HighTempErrFlag = 0;
            }
        }
    }
    else
    {
        if (u8_SensorErrFlag == 1      // 传感器故障
            || u8_HighTempErrFlag == 1 // 超温故障
            || u8_LowTempErrFlag == 1) // 超温故障
        {
            u8_SensorErrFlag = 0;
            u8_HighTempErrFlag = 0;
            u8_LowTempErrFlag = 0;
            u8_LedMenuPageIndex = MENU_PAGE_MAIN;
            u16_LedMenuDisTime = u8_DisplayTime_F3;
        }
    }
    // TestTemp1 = POWER_ON_TEMP;
    // TestTemp2 = POWER_OFF_TEMP;
    // if (u16_stADRoomTemp < (u16_SetTemp2AD_C0 - u8_TempUpReturnDiff_C1)) // 开机点判断

    if (u8_SensorErrFlag == 0)
    {
        if (s8_RealTemp > POWER_ON_TEMP) // 开机点判断
        {                                // 数字越大温度越低
            u8_OpenCompressorTempFlag = 1;
        }
        // else if (u16_stADRoomTemp > (u16_SetTemp2AD_C0 + u8_TempDownReturnDiff_C2)) // 关机点判断
        else if (s8_RealTemp < POWER_OFF_TEMP) // 关机点判断
        {                                      // 数字越大温度越低
            // u8_CloseCompressorTempFlag = 1;
            u8_OpenCompressorTempFlag = 0;

            if (u8_SonwLedFlashFlag != SONW_LED_OFF)
            {
                u8_SonwLedFlashFlag = SONW_LED_OFF;
            }
        }
    }
}

void CompressorControl(void)
{
    static uint8_t u8T_CompressorState = 0;

    GetSystemState();

    if (u8_SensorErrFlag)
    {
        if ((u8_SensorErrCompOnOff == 1) && ((u8_CompressorOffTimeFlag == 1) || (u8_Compressor1stOnTimeFlag == 1)) && (u8_DefrostState == 0))
        {
            u8_CompressorOffTimeFlag = 0;
            u16_CompressorOffTime2S = 0;
            u8_Compressor1stOnTimeFlag = 0;
            u8T_CompressorState = 1;
        }
        else if (u8_SensorErrCompOnOff == 0) // 关机
        {
            // u8_SensorErrCompOnOff = 0xFF;
            u8T_CompressorState = 0;
            if (u16_CompressorOffTime2S == 0 && u8_CompressorOffTimeFlag == 0)
            {
                u8_CompressorOffTimeFlag = 0;
                u16_CompressorOffTime2S = 1;
            }
        }
    }
    else
    {
        if ((u8_OpenCompressorTempFlag == 1) && (u8_Compressor1stOnTimeFlag == 1)) // 第一次上电压机开机
        {
            u8_Compressor1stOnTimeFlag = 0;
            u16_CompressorOffTime2S = 0;

            u8T_CompressorState = 1;
        }
        // 开机点要判断是不是过了最小开机时间还有是不是除霜模式
        else if ((u8_OpenCompressorTempFlag == 1) && (u8_CompressorOffTimeFlag == 1) && (u8_DefrostState == 0))
        {
            u8_CompressorOffTimeFlag = 0;
            u16_CompressorOffTime2S = 0;

            u8T_CompressorState = 1;
        }
        else if (u8_OpenCompressorTempFlag == 0) // 关机点关机
        {
            u8T_CompressorState = 0;
            if (u16_CompressorOffTime2S == 0 && u8_CompressorOffTimeFlag == 0)
            {
                u8_CompressorOffTimeFlag = 0;
                u16_CompressorOffTime2S = 1;
            }
        }
    }

    if (u8_DefrostState == 1)
    {
        u8T_CompressorState = 0;

        if (u16_CompressorOffTime2S == 0 && u8_CompressorOffTimeFlag == 0)
        {
            u8_CompressorOffTimeFlag = 0;
            u16_CompressorOffTime2S = 1;
        }
    }

    // if (u8T_CompressorState)
    // {
    //     u8_SonwLedFlashFlag = SONW_LED_ON;
    // }
    // else
    // {
    //     u8_SonwLedFlashFlag = SONW_LED_OFF;
    // }

    if (u8_CompressorState != u8T_CompressorState)
    {
        u8_CompressorState = u8T_CompressorState;
        if (u8_DefrostState == 0)
        {
            u8_SonwLedFlashFlag = u8T_CompressorState; // 0/1  SONW_LED_ON/SONW_LED_OFF
        }
        COMPRESSOR_ONOFF(u8T_CompressorState);
    }
}

uint8_t read_button_GPIO(uint8_t button_id)
{
    // you can share the GPIO read function with multiple Buttons
    switch (button_id)
    {
    case BTN1_ID:
        return GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6);
    default:
        return 0;
    }
}

void Key_Scan(void)
{
    // if ((uint32_t)(u32_systemTime - u32_ReadKeyTimer) >= 5)
    // {
    //     u32_ReadKeyTimer = u32_systemTime;
    // button_ticks();
    // }
    button_ticks();
}

void Key_Configure(void)
{
    button_init(&btn1, read_button_GPIO, 0, BTN1_ID);
    button_attach(&btn1, SINGLE_CLICK, BTN1_SINGLE_Click_Handler);
    button_attach(&btn1, PRESS_REPEAT, BTN1_SINGLE_Click_Handler);
    button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_PRESS_START_Handler);
    button_attach(&btn1, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);
    button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler);

    button_start(&btn1);
}

void BTN1_SINGLE_Click_Handler(void *btn)
{
    // do something...
    // u16_LedMenuDisTime = SET_MENU_TIME;
    // u8_NoKeyPressFlashMenuTime = NO_KEY_PRESS_MENU_FLASH_TIME; // 1s
    // u8_MenuFlahsFlag = 0;

    if (u8_LedMenuPageIndex == MENU_PAGE_PASSWORD)
    {
        if (u8_LedMenuItem == 1)
        {
            u8_PassWord++;
            if (u8_PassWord > 99)
            {
                u8_PassWord = 0;
            }
        }
    }
    else if (u8_LedMenuPageIndex == MENU_PAGE_FACTORY)
    {
        if (u8_LedMenuItem == 0)
        {
            u8_LedMenuItemIndex++;
            if (u8_LedMenuItemIndex > Item_F3)
            {
                u8_LedMenuItemIndex = Item_P0;
            }
        }
        else
        {
            switch (u8_LedMenuItemIndex)
            {
            case Item_P0:
            {
                u8_Password_P0++;
                if (u8_Password_P0 > 99)
                {
                    u8_Password_P0 = 0;
                }
            }
            break;
            case Item_C0:
            {
                s8_SetTempC0Display++;

                if (s8_SetTempC0Display > s8_SetTempUp_C5)
                {
                    s8_SetTempC0Display = s8_SetTempDown_C6;
                }
                // u8_TempIsSet = 1;
            }
            break;
            case Item_C1:
            {
                u8_TempUpReturnDiff_C1++;
                if (u8_TempUpReturnDiff_C1 > 99)
                {
                    u8_TempUpReturnDiff_C1 = 0;
                }
            }
            break;
            case Item_C2:
            {
                u8_TempDownReturnDiff_C2++;
                if (u8_TempDownReturnDiff_C2 > 99)
                {
                    u8_TempDownReturnDiff_C2 = 0;
                }
            }
            break;
            case Item_C3:
            {
                u8_CompressorOffTime_C3++;
                if (u8_CompressorOffTime_C3 > 99)
                {
                    u8_CompressorOffTime_C3 = 0;
                }
            }
            break;
            case Item_C4:
            {
                u8_Compressor1stOffTime_C4++;
                if (u8_Compressor1stOffTime_C4 > 99)
                {
                    u8_Compressor1stOffTime_C4 = 0;
                }
            }
            break;
            case Item_C5:
            {
                s8_SetTempUp_C5++;
                if (s8_SetTempUp_C5 > 55)
                {
                    s8_SetTempUp_C5 = s8_SetTemp_C0;
                }
            }
            break;
            case Item_C6:
            {
                s8_SetTempDown_C6++;
                if (s8_SetTempDown_C6 > s8_SetTemp_C0)
                {
                    s8_SetTempDown_C6 = -40;
                }
            }
            break;
            case Item_C7:
            {
                s8_TempComp_C7++;
                if (s8_TempComp_C7 > 99)
                {
                    s8_TempComp_C7 = -99;
                }
            }
            break;
            case Item_C8:
            {
                u8_SensorErrCompressorOnTime_C8++;
                if (u8_SensorErrCompressorOnTime_C8 > 99)
                {
                    u8_SensorErrCompressorOnTime_C8 = 0;
                }
            }
            break;
            case Item_C9:
            {
                u8_SensorErrCompressorOffTime_C9++;
                if (u8_SensorErrCompressorOffTime_C9 > 99)
                {
                    u8_SensorErrCompressorOffTime_C9 = 1;
                }
            }
            break;
            case Item_D1:
            {
                u8_DefrostCycle_D1++;
                if (u8_DefrostCycle_D1 > 99)
                {
                    u8_DefrostCycle_D1 = 0;
                }
            }
            break;
            case Item_D2:
            {
                u8_DefrostTime_D2++;
                if (u8_DefrostTime_D2 > 99)
                {
                    u8_DefrostTime_D2 = 1;
                }
            }
            break;
            case Item_A1:
            {
                s8_HighTempErr_A1++;
                if (s8_HighTempErr_A1 > 50)
                {
                    s8_HighTempErr_A1 = s8_LowTempErr_A2;
                }
            }
            break;
            case Item_A2:
            {
                s8_LowTempErr_A2++;
                if (s8_LowTempErr_A2 > s8_HighTempErr_A1)
                {
                    s8_LowTempErr_A2 = -40;
                }
            }
            break;
            case Item_A3:
            {
                u8_AboveTempContinueTime_A3++;
                if (u8_AboveTempContinueTime_A3 > 99)
                {
                    u8_AboveTempContinueTime_A3 = 0;
                }
            }
            break;
            case Item_F1:
            {
                u8_DisplayMode_F1++;
                if (u8_DisplayMode_F1 > 1)
                {
                    u8_DisplayMode_F1 = 0;
                }
            }
            break;
            case Item_F2:
            {
                u8_DisplayMode_F2++;
                if (u8_DisplayMode_F2 > 2)
                {
                    u8_DisplayMode_F2 = 0;
                }
            }
            break;
            case Item_F3:
            {
                u8_DisplayTime_F3++;
                if (u8_DisplayTime_F3 > 99)
                {
                    u8_DisplayTime_F3 = 0;
                }
            }
            break;

            default:
                break;
            }
        }
    }
}

/*
void BTN1_PRESS_REPEAT_Handler(void *btn)
{
    // do something...
    // u16_LedMenuDisTime = SET_MENU_TIME;
    // u8_NoKeyPressFlashMenuTime = NO_KEY_PRESS_MENU_FLASH_TIME; // 1s
    // u8_MenuFlahsFlag = 0;

    if (u8_LedMenuPageIndex == MENU_PAGE_SET)
    {
        s8_SetTemp_C0--;
        if (s8_SetTemp_C0 < s8_SetTempDown_C6)
        {
            s8_SetTemp_C0 = s8_SetTempUp_C5;
        }
        // u8_TempIsSet = 1;
    }
    else if (u8_LedMenuPageIndex == MENU_PAGE_PASSWORD)
    {
        if (u8_LedMenuItem == 1)
        {
            u8_PassWord++;
            if (u8_PassWord > 99)
            {
                u8_PassWord = 0;
            }
        }
    }
    else if (u8_LedMenuPageIndex == MENU_PAGE_FACTORY)
    {
        if (u8_LedMenuItem == 0)
        {
            u8_LedMenuItemIndex++;
            if (u8_LedMenuItemIndex > Item_F3)
            {
                u8_LedMenuItemIndex = Item_P0;
            }
        }
        else
        {
            switch (u8_LedMenuItemIndex)
            {
            case Item_P0:
            {
                u8_Password_P0++;
                if (u8_Password_P0 > 99)
                {
                    u8_Password_P0 = 0;
                }
            }
            break;
            case Item_C0:
            {
                s8_SetTemp_C0++;

                if (s8_SetTemp_C0 > s8_SetTempUp_C5)
                {
                    s8_SetTemp_C0 = s8_SetTempDown_C6;
                }
                // u8_TempIsSet = 1;
            }
            break;
            case Item_C1:
            {
                u8_TempUpReturnDiff_C1++;
                if (u8_TempUpReturnDiff_C1 > 99)
                {
                    u8_TempUpReturnDiff_C1 = 0;
                }
            }
            break;
            case Item_C2:
            {
                u8_TempDownReturnDiff_C2++;
                if (u8_TempDownReturnDiff_C2 > 99)
                {
                    u8_TempDownReturnDiff_C2 = 0;
                }
            }
            break;
            case Item_C3:
            {
                u8_CompressorOffTime_C3++;
                if (u8_CompressorOffTime_C3 > 99)
                {
                    u8_CompressorOffTime_C3 = 0;
                }
            }
            break;
            case Item_C4:
            {
                u8_Compressor1stOffTime_C4++;
                if (u8_Compressor1stOffTime_C4 > 99)
                {
                    u8_Compressor1stOffTime_C4 = 0;
                }
            }
            break;
            case Item_C5:
            {
                s8_SetTempUp_C5++;
                if (s8_SetTempUp_C5 > 55)
                {
                    s8_SetTempUp_C5 = s8_SetTemp_C0;
                }
            }
            break;
            case Item_C6:
            {
                s8_SetTempDown_C6++;
                if (s8_SetTempDown_C6 > s8_SetTemp_C0)
                {
                    s8_SetTempDown_C6 = -40;
                }
            }
            break;
            case Item_C7:
            {
                s8_TempComp_C7++;
                if (s8_TempComp_C7 > 99)
                {
                    s8_TempComp_C7 = -99;
                }
            }
            break;
            case Item_C8:
            {
                u8_SensorErrCompressorOnTime_C8++;
                if (u8_SensorErrCompressorOnTime_C8 > 99)
                {
                    u8_SensorErrCompressorOnTime_C8 = 0;
                }
            }
            break;
            case Item_C9:
            {
                u8_SensorErrCompressorOffTime_C9++;
                if (u8_SensorErrCompressorOffTime_C9 > 99)
                {
                    u8_SensorErrCompressorOffTime_C9 = 1;
                }
            }
            break;
            case Item_D1:
            {
                u8_DefrostCycle_D1++;
                if (u8_DefrostCycle_D1 > 99)
                {
                    u8_DefrostCycle_D1 = 0;
                }
            }
            break;
            case Item_D2:
            {
                u8_DefrostTime_D2++;
                if (u8_DefrostTime_D2 > 99)
                {
                    u8_DefrostTime_D2 = 1;
                }
            }
            break;
            case Item_A1:
            {
                s8_HighTempErr_A1++;
                if (s8_HighTempErr_A1 > 50)
                {
                    s8_HighTempErr_A1 = s8_LowTempErr_A2;
                }
            }
            break;
            case Item_A2:
            {
                s8_LowTempErr_A2++;
                if (s8_LowTempErr_A2 > s8_HighTempErr_A1)
                {
                    s8_LowTempErr_A2 = -40;
                }
            }
            break;
            case Item_A3:
            {
                u8_AboveTempContinueTime_A3++;
                if (u8_AboveTempContinueTime_A3 > 99)
                {
                    u8_AboveTempContinueTime_A3 = 0;
                }
            }
            break;
            case Item_F1:
            {
                u8_DisplayMode_F1++;
                if (u8_DisplayMode_F1 > 1)
                {
                    u8_DisplayMode_F1 = 0;
                }
            }
            break;
            case Item_F2:
            {
                u8_DisplayMode_F2++;
                if (u8_DisplayMode_F2 > 2)
                {
                    u8_DisplayMode_F2 = 0;
                }
            }
            break;
            case Item_F3:
            {
                u8_DisplayTime_F3++;
                if (u8_DisplayTime_F3 > 99)
                {
                    u8_DisplayTime_F3 = 0;
                }
            }
            break;

            default:
                break;
            }
        }
    }
}
*/

void BTN1_LONG_PRESS_START_Handler(void *btn)
{
    // do something...
    // u8_NoKeyPressFlashMenuTime = NO_KEY_PRESS_MENU_FLASH_TIME; // 1s
    // u8_MenuFlahsFlag = 0;
    // u16_LedMenuDisTime = SET_MENU_TIME;

    if (((u8_LedMenuPageIndex == MENU_PAGE_MAIN) && (u8_LedMenuItem == 0)) // 主菜单并且显示
        || (u8_LedMenuPageIndex == MENU_PAGE_ERR))                         // 取消传感器故障不响应 || (u8_LedMenuPageIndex == MENU_PAGE_ERR && u8_SensorErrFlag == 0)
    {
        // if (u8_HighTempErrFlag == 1    // 超温故障 取消-按键取消超温报警
        //     || u8_LowTempErrFlag == 1) // 超温故障
        // {
        //     u8_HighTempErrFlag = 0;
        //     u8_LowTempErrFlag = 0;
        // }
        u8_LedMenuPageIndex = MENU_PAGE_PASSWORD;
        u8_LedMenuItem = 0;
        u16_LedMenuDisTime = SET_MENU_TIME;
        u8_PassWord = 0; // 进入密码菜单数字清0
    }
    else if (u8_LedMenuPageIndex == MENU_PAGE_PASSWORD)
    {
        // if (u8_HighTempErrFlag == 1    // 超温故障  取消-按键取消超温报警
        //     || u8_LowTempErrFlag == 1) // 超温故障
        // {
        //     u8_HighTempErrFlag = 0;
        //     u8_LowTempErrFlag = 0;
        // }

        if (u8_LedMenuItem == 0)
        {
            u8_LongPress2ndActive = 0;
            u8_LedMenuItem = 1;
        }
    }
    else if (u8_LedMenuPageIndex == MENU_PAGE_FACTORY)
    {
        // if (u8_HighTempErrFlag == 1    // 超温故障  取消-按键取消超温报警
        //     || u8_LowTempErrFlag == 1) // 超温故障
        // {
        //     u8_HighTempErrFlag = 0;
        //     u8_LowTempErrFlag = 0;
        // }

        if (u8_LedMenuItem == 0)
        {
            u8_LongPress2ndActive = 0;
            u8_LedMenuItem = 1;
        }
    }
}

void BTN1_LONG_PRESS_HOLD_Handler(void *btn)
{
    // do something...

    u16_500msDelay++;
    // if ((u8_LedMenuPageIndex == MENU_PAGE_MAIN) && (u8_LedMenuItem == 0))
    // {
    //     u8_NoKeyPressFlashMenuTime = NO_KEY_PRESS_MENU_FLASH_TIME; // 1s
    //     u8_MenuFlahsFlag = 0;
    //     u8_MenuFlahsCycle = 0;
    //     u16_LedMenuDisTime = SET_MENU_TIME;
    // }
    if (u16_500msDelay % LONG_KEY_PRESS_TIME == 0) // 定时中断5ms中调用，所以100为500ms
    {
        // u16_500msDelay = 0;

        if (u8_LedMenuPageIndex == MENU_PAGE_SET)
        {
            s8_SetTempC0Display--;
            if (s8_SetTempC0Display < s8_SetTempDown_C6)
            {
                s8_SetTempC0Display = s8_SetTempUp_C5;
            }
            // u8_TempIsSet = 1;
        }
        else if (u8_LedMenuPageIndex == MENU_PAGE_PASSWORD)
        {
            if ((u8_LedMenuItem == 1) && (u8_LongPress2ndActive == 1))
            {
                u8_PassWord++;
                if (u8_PassWord > 99)
                {
                    u8_PassWord = 0;
                }
            }
        }
        else if (u8_LedMenuPageIndex == MENU_PAGE_FACTORY)
        {
            if ((u8_LedMenuItem == 1) && (u8_LongPress2ndActive == 1))
            {
                switch (u8_LedMenuItemIndex)
                {
                case Item_P0:
                {
                    u8_Password_P0++;
                    if (u8_Password_P0 > 99)
                    {
                        u8_Password_P0 = 0;
                    }
                }
                break;
                case Item_C0:
                {
                    s8_SetTempC0Display++;

                    if (s8_SetTempC0Display > s8_SetTempUp_C5)
                    {
                        s8_SetTempC0Display = s8_SetTempDown_C6;
                    }
                    // u8_TempIsSet = 1;
                }
                break;
                case Item_C1:
                {
                    u8_TempUpReturnDiff_C1++;
                    if (u8_TempUpReturnDiff_C1 > 99)
                    {
                        u8_TempUpReturnDiff_C1 = 0;
                    }
                }
                break;
                case Item_C2:
                {
                    u8_TempDownReturnDiff_C2++;
                    if (u8_TempDownReturnDiff_C2 > 99)
                    {
                        u8_TempDownReturnDiff_C2 = 0;
                    }
                }
                break;
                case Item_C3:
                {
                    u8_CompressorOffTime_C3++;
                    if (u8_CompressorOffTime_C3 > 99)
                    {
                        u8_CompressorOffTime_C3 = 0;
                    }
                }
                break;
                case Item_C4:
                {
                    u8_Compressor1stOffTime_C4++;
                    if (u8_Compressor1stOffTime_C4 > 99)
                    {
                        u8_Compressor1stOffTime_C4 = 0;
                    }
                }
                break;
                case Item_C5:
                {
                    s8_SetTempUp_C5++;
                    if (s8_SetTempUp_C5 > 55)
                    {
                        s8_SetTempUp_C5 = s8_SetTemp_C0;
                    }
                }
                break;
                case Item_C6:
                {
                    s8_SetTempDown_C6++;
                    if (s8_SetTempDown_C6 > s8_SetTemp_C0)
                    {
                        s8_SetTempDown_C6 = -40;
                    }
                }
                break;
                case Item_C7:
                {
                    s8_TempComp_C7++;
                    if (s8_TempComp_C7 > 99)
                    {
                        s8_TempComp_C7 = -99;
                    }
                }
                break;
                case Item_C8:
                {
                    u8_SensorErrCompressorOnTime_C8++;
                    if (u8_SensorErrCompressorOnTime_C8 > 99)
                    {
                        u8_SensorErrCompressorOnTime_C8 = 0;
                    }
                }
                break;
                case Item_C9:
                {
                    u8_SensorErrCompressorOffTime_C9++;
                    if (u8_SensorErrCompressorOffTime_C9 > 99)
                    {
                        u8_SensorErrCompressorOffTime_C9 = 1;
                    }
                }
                break;
                case Item_D1:
                {
                    u8_DefrostCycle_D1++;
                    if (u8_DefrostCycle_D1 > 99)
                    {
                        u8_DefrostCycle_D1 = 0;
                    }
                }
                break;
                case Item_D2:
                {
                    u8_DefrostTime_D2++;
                    if (u8_DefrostTime_D2 > 99)
                    {
                        u8_DefrostTime_D2 = 1;
                    }
                }
                break;
                case Item_A1:
                {
                    s8_HighTempErr_A1++;
                    if (s8_HighTempErr_A1 > 50)
                    {
                        s8_HighTempErr_A1 = s8_LowTempErr_A2;
                    }
                }
                break;
                case Item_A2:
                {
                    s8_LowTempErr_A2++;
                    if (s8_LowTempErr_A2 > s8_HighTempErr_A1)
                    {
                        s8_LowTempErr_A2 = -40;
                    }
                }
                break;
                case Item_A3:
                {
                    u8_AboveTempContinueTime_A3++;
                    if (u8_AboveTempContinueTime_A3 > 99)
                    {
                        u8_AboveTempContinueTime_A3 = 0;
                    }
                }
                break;
                case Item_F1:
                {
                    u8_DisplayMode_F1++;
                    if (u8_DisplayMode_F1 > 1)
                    {
                        u8_DisplayMode_F1 = 0;
                    }
                }
                break;
                case Item_F2:
                {
                    u8_DisplayMode_F2++;
                    if (u8_DisplayMode_F2 > 2)
                    {
                        u8_DisplayMode_F2 = 0;
                    }
                }
                break;
                case Item_F3:
                {
                    u8_DisplayTime_F3++;
                    if (u8_DisplayTime_F3 > 99)
                    {
                        u8_DisplayTime_F3 = 0;
                    }
                }
                break;

                default:
                    break;
                }
            }
        }
    }
}

void BTN1_PRESS_UP_Handler(void *btn)
{
    if ((u8_LedMenuPageIndex == MENU_PAGE_MAIN) && (u8_LedMenuItem == 1))
    {
        u8_LedMenuItem = 0;
        u16_LedMenuDisTime = u8_DisplayTime_F3;
    }
    else if (((u8_LedMenuPageIndex == MENU_PAGE_MAIN) && (u8_LedMenuItem == 0)) // 主界面并且显示
             || (u8_LedMenuPageIndex == MENU_PAGE_ERR))                         // 取消传感器故障不响应按键|| (u8_LedMenuPageIndex == MENU_PAGE_ERR && u8_SensorErrFlag == 0)
    {
        // if (u8_HighTempErrFlag == 1    // 超温故障 取消-按键取消超温报警
        //     || u8_LowTempErrFlag == 1) // 超温故障
        // {
        //     u8_HighTempErrFlag = 0;
        //     u8_LowTempErrFlag = 0;
        // }
        u8_LedMenuPageIndex = MENU_PAGE_SET;
        u16_LedMenuDisTime = SET_MENU_TIME;
        // u8_TempIsSet = 1;
    }
    else if (u8_LedMenuPageIndex == MENU_PAGE_SET)
    {
        s8_SetTempC0Display--;
        if (s8_SetTempC0Display < s8_SetTempDown_C6)
        {
            s8_SetTempC0Display = s8_SetTempUp_C5;
        }
        // u8_TempIsSet = 1;
    }
    else if (((u8_LedMenuPageIndex == MENU_PAGE_PASSWORD) && (u8_LedMenuItem == 1))    // 密码菜单进入输入密码界面必须抬起按键长按功能才有效
             || ((u8_LedMenuPageIndex == MENU_PAGE_FACTORY) && (u8_LedMenuItem == 1))) // 工程菜单各子项进入界面必须抬起按键长按功能才有效
    {
        u8_LongPress2ndActive = 1;
    }
}
